﻿using EventMgrLib;
using SmartMotion.Core;
using SmartMotion.Core.Mgrs;
using SmartMotion.Product;
using SmartMotion.Properties;
using Sunny.UI;
using System;
using System.Collections.Concurrent;
using System.Windows.Forms;

namespace SmartMotion.AllPages
{
    public partial class FrmProductMgr : UIPage
    {
        private ConcurrentDictionary<string, VarInfo> _vars;
        private EventMgr _em;
        private ProductMgr _pm;

        public FrmProductMgr()
        {
            InitializeComponent();
        }

        private void FrmProductMgr_Load(object sender, EventArgs e)
        {
            _vars = VarMgr.Ins.Vars[VarMgr.ProductPars];//从大字典里拿小字典
            _em = EventMgr.Ins;
            _pm = ProductMgr.Ins;
            _em.GetEvent<ProductEvent>().Subscribe(ProductOpt);

            SetGrid();
            BindingData();
        }

        private void ProductOpt(FileOpt fileOpt)
        {
            switch (fileOpt)
            {
                case FileOpt.None:
                    break;

                case FileOpt.Create:
                    _pm.New();
                    break;

                case FileOpt.Open:
                    _pm.Open();
                    _vars = VarMgr.Ins.Vars[VarMgr.ProductPars];
                    BindingData();
                    SavePath();
                    break;

                case FileOpt.Save:
                    _pm.Save();
                    SavePath();
                    break;

                case FileOpt.SaveAs:
                    _pm.SaveAs();
                    SavePath();
                    break;

                case FileOpt.Add:
                    var frmParAdd = new frmParAdd();
                    if (frmParAdd.ShowDialog() == DialogResult.OK)
                    {
                        if (_vars.ContainsKey(frmParAdd.Result.Name))
                        {
                            ShowErrorDialog("参数已存在");
                        }
                        else
                        {
                            _vars.TryAdd(frmParAdd.Result.Name, frmParAdd.Result);
                            BindingData();
                        }
                    }
                    break;

                case FileOpt.Del:
                    var dataGridViewRow = this.dgv.CurrentRow;
                    if (dataGridViewRow != null)
                    {
                        if (!(dataGridViewRow.DataBoundItem is VarInfo par))
                        {
                            return;
                        }

                        _vars.TryRemove(par.Name, out _);
                    }

                    BindingData();
                    break;

                case FileOpt.Edit:
                    break;
            }
        }

        private void SetGrid()
        {
            dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgv.RowHeadersVisible = false;
            dgv.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgv.AllowUserToResizeRows = false;
            dgv.ReadOnly = true;
            dgv.MultiSelect = false;
            dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void BindingData()
        {
            var bs = new BindingSource();
            bs.DataSource = _vars.Values;
            dgv.DataSource = bs;
        }

        private void SavePath()
        {
            Settings.Default.ProductFilePath = _pm.FilePath;
            Settings.Default.Save();
        }

        private void FrmProductMgr_FormClosed(object sender, FormClosedEventArgs e)
        {
        }

        private void FrmProductMgr_FormClosing(object sender, FormClosingEventArgs e)
        {
            _em.GetEvent<ProductEvent>().Unsubscribe(ProductOpt);
        }
    }
}
